package com.dkm.orproject.dkmoa.action;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.dkm.orproject.dkmoa.domain.OaClient;
import com.dkm.orproject.dkmoa.domain.OaLog;
import com.dkm.orproject.dkmoa.domain.OaPersonnel;
import com.dkm.orproject.dkmoa.domain.OaRegime;
import com.dkm.orproject.dkmoa.service.OaClientService;
import com.dkm.orproject.utils.jwt.login.UserLoginToken;
import com.dkm.orproject.utils.utils.ThreeVo;
import com.dkm.orproject.utils.utils.Tools;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 *  @ClassName : OaClientAction  //类名
 *  @Description : 客户控制层
 *  @Author : zmh //作者
 *  @Date: 2020-11-17 09:28  //时间
 */
@RestController
@RequestMapping("/OaClientAction")
public class OaClientAction {

    /**
     * 工具类
     */
    @Resource
    Tools tools;

    /**
     * 客户service
     */
    @Resource
    OaClientService oaClientService;


    /**
     * 修改和增加客户的接口
     * @param oaClient 客户模型对象
     * @param request 请求对象
     * @return 回调函数
     */
    @PostMapping("addOrUpdate")
    @UserLoginToken
    public Map<String,Object> addOrUpdate(OaClient oaClient, HttpServletRequest request,Long select){
        OaPersonnel oaPersonnel = (OaPersonnel) request.getSession().getAttribute("user");
        Map<String,Object> map = new HashMap<>();
        if(ObjectUtils.isEmpty(oaPersonnel)){
          tools.rowMap(map, 0, null);
        }try {
       int rows = 0;
       OaLog log = new OaLog();
       if(!StringUtils.isEmpty(select)){
           oaClient.setClientTypeId(select);
       }
       if(oaClient.getPkClientId()!=null && !"".equals(oaClient.getPkClientId())){
           oaClient.setUpdatedBy(oaPersonnel.getPersonnelName());
           oaClient.setUpdatedTime(new Date());
           log.setLogTitle("修改客户");
          rows = oaClientService.updateByPrimaryKeySelective(oaClient);
       }else {
           oaClient.setCreatedBy(oaPersonnel.getPersonnelName());
           oaClient.setIsRevision("1");
           log.setLogTitle("增加客户");
           rows = oaClientService.insertOaClient(oaClient);
       }
            log.setCreatedBy(oaPersonnel.getPersonnelName());
            log.setLogIp(request.getRemoteAddr());
            log.setLogBody(JSONObject.toJSON(oaPersonnel).toString());
            log.setLogType("1");
            tools.rowMap(map,rows,log);
            return map;
      }catch (Exception e){
            OaLog log = new OaLog();
            log.setLogBody(JSONObject.toJSON(e.getStackTrace()).toString());
            log.setLogTitle("客户增加或修改模块异常");
            log.setCreatedBy(oaPersonnel.getPersonnelName());
            log.setLogType("0");
            log.setLogIp(request.getRemoteAddr());
            tools.errorMap(map, log);
            return map;
        }
    }

    /**
     *根据Id删除该条数据接口
     * @param pkOaClientId
     * @param session 服务器会话
     * @param request 服务器请求
     * @return 回调函数
     */

    @DeleteMapping("deleteOaClientById/{pkClientId}")
    @UserLoginToken
    public Map<String, Object> deleteByPrimaryKey(@PathVariable("pkClientId") Long pkOaClientId, HttpSession session, HttpServletRequest request) {
        OaPersonnel oaPersonnel = (OaPersonnel) session.getAttribute("user");
        Map<String, Object> map = new HashMap<>();
        try {
            OaLog log = new OaLog();
            OaClient oaClient = new OaClient();
            oaClient.setPkClientId(pkOaClientId);
            oaClient.setIsRevision("0");
            log.setCreatedBy(oaPersonnel.getPersonnelName());
            log.setLogTitle("客户的删除模块");
            int rows = oaClientService.deleteById(oaClient);
            log.setLogIp(request.getRemoteAddr());
            log.setLogType("1");
            log.setLogBody(JSON.toJSON(oaPersonnel).toString());
            tools.rowMap(map, rows, log);
            return map;
        } catch (Exception e) {
            OaLog log = new OaLog();
            log.setLogTitle("客户的删除模块异常");
            log.setLogIp(request.getRemoteAddr());
            log.setLogBody(JSON.toJSON(e.getStackTrace()).toString());
            log.setCreatedBy(oaPersonnel.getPersonnelName());
            log.setLogType("0");
            tools.errorMap(map, log);
            return map;
        }

    }

    /**
     * 根据Id查询接口
     * @param pkClientId 客户id值
     * @param request 请求对象
     * @return 客户模型
     */
    @GetMapping("findById/{pkClientId}")
    @UserLoginToken
    public Map<String, Object> selectByPrimaryKey(@PathVariable("pkClientId") Long pkClientId, HttpServletRequest request) {
        OaPersonnel oaPersonnel = (OaPersonnel) request.getSession().getAttribute("user");
        Map<String, Object> map = new HashMap<>();
        try {
            OaClient oaClient = oaClientService.selectByPrimaryKey(pkClientId);
            OaLog log = new OaLog();
            log.setLogTitle("客户id查询模块查询成功");
            log.setCreatedBy(oaPersonnel.getPersonnelName());
            log.setLogIp(request.getRemoteAddr());
            log.setLogType("1");
            log.setLogBody(JSON.toJSON(oaPersonnel).toString());
            tools.rowMap(map, 1, null);
            map.put("data", oaClient);
            return map;
        } catch (Exception e) {
            OaLog log = new OaLog();
            log.setLogTitle("客户根据id查询模块异常");
            log.setLogBody(JSON.toJSON(e.getStackTrace()).toString());
            log.setCreatedBy(oaPersonnel.getPersonnelName());
            log.setLogType("0");
            log.setLogIp(request.getRemoteAddr());
            tools.errorMap(map, log);
        }
        return map;
    }

    /**
     * 分页查询所有客户的接口
     * @param oaClient
     * @param page 当前页数
     * @param limit 每页的行数
     * @param startTime 开始时间
     * @param endDate 结束时间
     * @param request 服务器请求对象
     * @return 所有客户的集合
     */

    @GetMapping("selectAll")
    @UserLoginToken
    public Map<String,Object> findAll(OaClient oaClient, Integer page, Integer limit, String startTime, String endDate, HttpServletRequest request){
        try {
            //查询总行数
            Long rowCount=oaClientService.rowCount(oaClient,startTime,endDate);
            //分页查询集合
            List<OaClient> logs=oaClientService.selectAll(oaClient,startTime,endDate,page,limit);
            //返回集合
            return Tools.maps(logs,rowCount);
        } catch (Exception e) {
            OaPersonnel oaPersonnel= (OaPersonnel)request.getSession().getAttribute("user");
            Map<String,Object> map=new HashMap<>();
            OaLog log = new OaLog();
            log.setLogBody(JSONObject.toJSON(e.getStackTrace()).toString());
            log.setLogTitle("查询客户的所有方法");
            log.setLogType("0");
            log.setLogIp(request.getRemoteAddr());
            log.setCreatedBy(oaPersonnel.getPersonnelName());
            tools.errorMap(map,log);
            return map;
        }
    }


    /**
     * 批量删除客户的接口
     * @param ids 要删除的客户id数组
     * @param session 服务器会话对象
     * @param request 服务器请求对象
     * @return 回调函数
     */
    @DeleteMapping("deleteAll")
    @UserLoginToken
    public Map<String, Object> deleteAll(@RequestParam(value = "ids[]", required = false) Long[] ids, HttpSession session, HttpServletRequest request) {
        OaPersonnel user = (OaPersonnel) session.getAttribute("user");
        List<Long> list;
        Map<String,Object> map=new HashMap<>();
        if (ObjectUtils.isEmpty(ids)){
            return tools.rowMap(map,0,null);
        }else {
            //把数组转化为集合
            list = Arrays.asList(ids);
        }
        try {
            OaLog log = new OaLog();
            int deleteAll = oaClientService.deleteAll(list);
            log.setLogType("1");
            log.setLogIp(request.getRemoteAddr());
            log.setLogBody(JSONObject.toJSON(user).toString());
            log.setLogTitle("客户批量删除模块");
            log.setCreatedBy(user.getPersonnelName());
            tools.rowMap(map, deleteAll, log);
            map.put("data",deleteAll);
            return map;
        } catch (Exception e) {
            OaLog log = new OaLog();
            log.setCreatedBy(user.getPersonnelName());
            log.setLogIp(request.getRemoteAddr());
            log.setLogType("0");
            log.setLogBody(JSON.toJSON(e.getStackTrace()).toString());
            log.setLogTitle("客户删除模块异常");
            tools.errorMap(map,log);
            return map;
        }
    }



    /**
     * 日期格式化
     *
     * @param binder
     */
    @InitBinder
    public void initBinder(WebDataBinder binder) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        // true:允许输入空值，false:不能为空值
        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
    }
}
